package com.health.mapp.impl;

import org.apache.commons.lang3.StringUtils;

import io.rong.models.TokenReslut;

import com.health.commons.MD5Password;
import com.health.commons.PropertiesService;
import com.health.db.dto.DoctorListDto;
import com.health.db.dto.StuffDetailDto;
import com.health.db.model.UserLIST;
import com.health.db.model.UserLISTTemp;
import com.health.db.service.sm.IRongService;
import com.health.db.service.user.IStuffListService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.DocLoginReq;
import com.health.mapp.bean.DocLoginResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * 
 * @author HiRoad
 *
 */
public class DocLoginImpl implements ActionInterface {
	private IUserLISTService userLISTService  ;
	private IStuffListService stuffListService;
	private IRongService rongService;
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		DocLoginReq req = (DocLoginReq) reqBean;
		DocLoginResp _resp = new DocLoginResp();

		String userNo=req.getUserPhone();
		String passWord = req.getPassword();
		
		//check the userid
		StuffDetailDto stuffDetail = stuffListService.selectByStuffNo(userNo);
		
		//登录信息
		UserLIST userLISTForUpdate = new UserLIST();
		//check the userid
		if(null!=stuffDetail)
		{
			
			DoctorListDto doctorListDto = packageDoctorListDto(stuffDetail);
			UserLISTTemp userLISTTemp = packageUserListDto(stuffDetail);
			
			if (stuffDetail.getPassword().startsWith("01000000")) {
				
				UserLIST updateUser = new UserLIST();
				updateUser.setPassword(MD5Password.getEncodedPassword_Hex(passWord));
				updateUser.setUserid(stuffDetail.getStuffid());
				updateUser.setPasswordCreateDate(DateUtils.now());
				updateUser.setUserNo(userNo);
				userLISTService.updatePassword(updateUser);
			}else{
				
				//密码校验
				if (!MD5Password.getEncodedPassword_Hex(passWord).equals(stuffDetail.getPassword())) {
					_resp.setErrorCode(ResultCode.LOGIN_FAIL_PASSWORD_IS_ERROR);
					_resp.setErrorMsg(ResultCode.LOGIN_FAIL_PASSWORD_IS_ERROR_NOTE);
					return _resp;
				}
			}
			
			if(StringUtils.isBlank(stuffDetail.getAppToken()))
			{
				String iconUrl=stuffDetail.getIconUrl();
				
				if (StringUtils.isBlank(iconUrl)) {
					iconUrl=PropertiesService.getString("iconUrl.docDefaultUrl").trim();
				}
				
				/**
				 * token为空的话重新从融云获取Token
				 * */
				TokenReslut tokenReslut=rongService.getUserToken(String.valueOf(stuffDetail.getStuffid()), stuffDetail.getStuffName(), iconUrl);
				if(null!=tokenReslut&&tokenReslut.getCode()==200)
				{
					
					userLISTForUpdate.setApptoken(tokenReslut.getToken());
					_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
					_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
					_resp.setUserId(String.valueOf(stuffDetail.getStuffid()));
					_resp.setToken(tokenReslut.getToken());
					_resp.setDoctor(doctorListDto);
					_resp.setUser(userLISTTemp);
					
				}else {
					_resp.setErrorCode(-1);
					_resp.setErrorMsg("获取Token失败");
				}
			}else
			{
				_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
				_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
				_resp.setUserId(String.valueOf(stuffDetail.getStuffid()));
				_resp.setToken(stuffDetail.getAppToken());
				_resp.setDoctor(doctorListDto);
				_resp.setUser(userLISTTemp);
			}
			
			
			userLISTForUpdate.setUserid(Long.valueOf(stuffDetail.getStuffid()));
			userLISTForUpdate.setLoginDatetime(DateUtils.now());
			userLISTForUpdate.setUserNo(userNo);
			 
			userLISTService.updateLoginInfo(userLISTForUpdate);

		}
		else
		{
			
			_resp.setErrorCode(ResultCode.LOGIN_FAIL_MEMBER_IS_NOT_EXISTS);
			_resp.setErrorMsg(ResultCode.LOGIN_FAIL_MEMBER_IS_NOT_EXISTS_NOTE);
		}
		
		
		return _resp;

	}
	
	/**
	 * javabean转换
	 * @param stuffDetail
	 * @return
	 */
	private DoctorListDto packageDoctorListDto(StuffDetailDto stuffDetail){
		DoctorListDto doctorListDto = new DoctorListDto();
		
		doctorListDto.setDocId(String.valueOf(stuffDetail.getStuffid()));
		
		doctorListDto.setDocName( stuffDetail.getStuffName()==null?"":stuffDetail.getStuffName());
		doctorListDto.setDocTitle(stuffDetail.getStuffTitle()==null?"": stuffDetail.getStuffTitle());
		doctorListDto.setDocLabel(stuffDetail.getStuffLabel()==null?"": stuffDetail.getStuffLabel());
		doctorListDto.setDocImageUrl( stuffDetail.getIconUrl()==null?"":stuffDetail.getIconUrl());
		doctorListDto.setHospitalId( stuffDetail.getEntityid()==null?0:stuffDetail.getEntityid());
		doctorListDto.setHospitalName( stuffDetail.getHospitalName()==null?"":stuffDetail.getHospitalName());
		doctorListDto.setDepartmentId( stuffDetail.getDepartmentid()==null?0:stuffDetail.getDepartmentid());
		doctorListDto.setDepartmentName(stuffDetail.getDepartmentName()==null?"": stuffDetail.getDepartmentName());
		doctorListDto.setConsultScope( stuffDetail.getConsultRecommend()==null?"":stuffDetail.getConsultRecommend());
		//recordAccessRight
		doctorListDto.setRecordAccessRight(stuffDetail.getRegisterWhere().contains(String.valueOf(Constant.CONSTANTS_ORDER_CHANNEL_ID_9.intValue()))
				?1:0);
		return doctorListDto;
	}
	
	private UserLISTTemp packageUserListDto(StuffDetailDto stuffDetail){
		
		UserLISTTemp user = new UserLISTTemp();
		user.setUserId(String.valueOf(stuffDetail.getStuffid()));
		user.setUserName(stuffDetail.getStuffName());
		user.setUserPhone(stuffDetail.getStuffNo());
		user.setRegTime(stuffDetail.getCreateDate());
		user.setIcon(stuffDetail.getIconUrl());
		
		return user;
	}

	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}

	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}

	public IStuffListService getStuffListService() {
		return stuffListService;
	}

	public void setStuffListService(IStuffListService stuffListService) {
		this.stuffListService = stuffListService;
	}

	/**
	 * @return the rongService
	 */
	public IRongService getRongService() {
		return rongService;
	}

	/**
	 * @param rongService the rongService to set
	 */
	public void setRongService(IRongService rongService) {
		this.rongService = rongService;
	}
	
	
	
}
